********************************************************************************************************************
*****Do-file for Table 6 and Tables A11-12
*****Who is mobilized to vote by short text messages? Evidence from a nationwide field experiment with young voters
********************************************************************************************************************
*****Last edited 24/6/5
********************************************************************************************************************
*****Ado packages needed: estout
********************************************************************************************************************


clear all

*Programs to calculate group differences with standard errors and stars
capture program drop, myrepost
program myrepost, eclass
ereturn repost b=`1'
ereturn repost V=`2'
ereturn scalar df_r=`3'
end

capture program drop mystars
program mystars, eclass
local d_stars=string(`1', "%9.3f")
local pval=ttail(`3',abs(`1'/`2'))*2

if `pval'<=0.01 {
local d_stars="`d_stars'"+"***"		
}

if `pval'>0.01 & `pval'<=0.05  {
local d_stars="`d_stars'"+"**"		
}

if `pval'>0.05 & `pval'<=0.1  {
local d_stars="`d_stars'"+"*"		
}

local se_stars=string(`2', "%9.3f")

local se_stars="("+"`se_stars'"+")"

ereturn local d_stars="`d_stars'"
ereturn local se_stars="`se_stars'"


end

*Use data
use \data\dataforanalysis230522_v2.dta, clear
*Set basecategory
fvset base 8 moses1d


gen treatedfa=1 if ntreatedf>0 & treated==. &  ntreatedf!=.
replace treatedfa=0 if ncontrolf>0 & treated==. & ntreatedf<1 & ncontrolf!=. 

gen totpottreat=ntreatedf+ncontrolf
bys petu20: gen fiid=_n

tab totpottreat if treatedfa!=.
tab totpottreat if treatedfa!=. & fiid==1

reg voted22 treatedfa molincome age i.moses1d firstvote i.mohighschool female foreign, cluster(kunta19)


gen treatedf_i=1 if ntreatedf==1 & treated==1
replace treatedf_i=0 if ncontrolf==1 & treated==0

gen notsamehh=1 if treatedf14!=. & treatedf==.
replace treatedf=treatedf14 if notsamehh==1
gen old=1 if age>49 & age!=.
replace old=0 if age<50 & age!=.

label variable treatedf_i "Treatments Pooled"
label variable treatedf "Spillover Treatment"
label variable treated1 "Neutral Treatment"
label variable treated2 "Expressive Treatment"
label variable treated3 "Informative Treatment"


*************************************************
*****TABLE A11
*************************************************

eststo clear
eststo M1: reg voted22 treatedf_i, cluster(kunta19)
estadd local Controls "No"
estadd local Controls1 " "
estadd local Controls2 " "
estadd local Controls3 " "
estadd local Controls4 " "
estadd local Controls5 "No"
mean voted22 if e(sample)==1 & treatedf_i==0
estadd scalar umean=r(table)[1,1]: M1
eststo M2: reg voted22 treatedf_i molincome female age foreign, cluster(kunta19)
estadd local Controls "Female, Age, Immigrant,"
estadd local Controls1 "Ln Income"
estadd local Controls2 " "
estadd local Controls3 " "
estadd local Controls4 " "
estadd local Controls5 "No"
mean voted22 if e(sample)==1 & treatedf_i==0
estadd scalar umean=r(table)[1,1]: M2
eststo M3:  reg voted22 treatedf_i molincome female age foreign i.moses1d firstvote i.mohighschool, cluster(kunta19)
estadd local Controls "Female, Age, Immigrant,"
estadd local Controls1 "Ln Income,"
estadd local Controls2 "SES Background,"
estadd local Controls3 "Educational Background,"
estadd local Controls4 "First Time Eligble to Vote"
estadd local Controls5 "No"
mean voted22 if e(sample)==1 & treatedf_i==0
estadd scalar umean=r(table)[1,1]: M3
eststo M4:  reg voted22 treatedf_i i.kunta19, cluster(kunta19)
estadd local Controls "No"
estadd local Controls1 " "
estadd local Controls2 " "
estadd local Controls3 " "
estadd local Controls4 " "
estadd local Controls5 "Yes"
mean voted22 if e(sample)==1 & treatedf_i==0
estadd scalar umean=r(table)[1,1]: M4
eststo M5:  reg voted22 treatedf_i molincome female age foreign i.moses1d firstvote i.mohighschool i.kunta19, cluster(kunta19)
estadd local Controls "Female, Age, Immigrant,"
estadd local Controls1 "Ln Income,"
estadd local Controls2 "SES Background,"
estadd local Controls3 "Educational Background,"
estadd local Controls4 "First Time Eligble to Vote"
estadd local Controls5 "Yes"
mean voted22 if e(sample)==1 & treatedf_i==0
estadd scalar umean=r(table)[1,1]: M5
local header "& \multicolumn{4}{c}{Outcome: Voted} \\"
local numbers "& (1) & (2) & (3) & (4) & (5) \\ \hline"
local emptyrow "& & & & &  \\ "
local line "& & & & & \hline \\ "

local dstars " "
local sestars " "
esttab, keep (treatedf_i) noobs label se b(%9.3f) se(%9.3f) star(* 0.10 ** 0.05 *** 0.01) ///
scalars("Controls Controls" "Controls1 \phantom{controls}"  "Controls2 \phantom{controls}" "Controls3 \phantom{controls}" "Controls4 \phantom{controls}"  "Controls5 Municipality FE" "umean Untreated $\bar{Y}$" "N Observations") ///
sfmt(%9.3f %9.3f %9.3f %9.3f  %9.3f  %9.3f  %9.3f %9.0fc) mlabels(none) nonumbers posthead("`header'" "`emptyrow'" "`numbers'" "`emptyrow'") ///
refcat(treatedf_i "", nolabel below) title("Average Treatment Effect") nonotes replace

*************************************************
*****TABLE 6
*************************************************



eststo clear


eststo old: reg voted22 treatedf molincome age i.moses1d firstvote i.mohighschool female foreign if old==1 & notsamehh!=1, cluster(kunta19)
matrix Brep1=e(b)
matrix Vrep1=e(V)
scalar df1=e(df_r)
estadd local controls "Yes"
estimates store old
qui: mean voted22 if treatedf==0 & e(sample)==1
estadd scalar umean=r(table)[1,1]: old




eststo young: reg voted22 treatedf molincome age i.moses1d firstvote i.mohighschool female foreign if old==0 & notsamehh!=1, cluster(kunta19)

matrix Brep2=e(b)
matrix Vrep2=e(V)
scalar df2=e(df_r)

scalar df=df1+df2

scalar diffe=Brep1[1,1]-Brep2[1,1]
scalar stde=sqrt(Vrep1[1,1]+Vrep2[1,1])
mystars diffe stde df
local dstars1=e(d_stars)
local sestars1=e(se_stars)
estadd local controls "Yes"
estimates store young
qui: mean voted22 if treatedf==0 & e(sample)==1
estadd scalar umean=r(table)[1,1]: young


eststo notsamehhold: reg voted22 treatedf molincome female age i.moses1d firstvote i.mohighschool foreign if notsamehh==1 & old==1, cluster(kunta19)

matrix Brep1=e(b)
matrix Vrep1=e(V)
scalar df1=e(df_r)
estadd local controls "Yes"
estimates store notsamehhold
qui: mean voted22 if treatedf==0 & e(sample)==1
estadd scalar umean=r(table)[1,1]: notsamehhold




eststo notsamehhyoung: reg voted22 treatedf molincome female age i.moses1d firstvote i.mohighschool foreign  if notsamehh==1 & old==0, cluster(kunta19)

matrix Brep2=e(b)
matrix Vrep2=e(V)
scalar df2=e(df_r)

scalar df=df1+df2

scalar diffe=Brep1[1,1]-Brep2[1,1]
scalar stde=sqrt(Vrep1[1,1]+Vrep2[1,1])
mystars diffe stde df
local dstars2=e(d_stars)
local sestars2=e(se_stars)
estadd local controls "Yes"
estimates store notsamehhyoung
qui: mean voted22 if treatedf==0 & e(sample)==1
estadd scalar umean=r(table)[1,1]: notsamehhyoung


local header "& \multicolumn{4}{c}{Outcome: Voted} \\"
local titles1 "& \multicolumn{2}{c}{{Currently living in the HH} & \multicolumn{2}{c}{Currently not living in the HH} \\ "
local titles2 "& Over 49 & Under 50 & Over 49 & Under 50\\"
local numbers "& (1) & (2) & (3) & (4) \\ \hline"
local emptyrow "& & & &  \\ "
local line "& & & & \hline \\ "

esttab, keep (treatedf) noobs label se b(%9.3f) se(%9.3f) star(* 0.10 ** 0.05 *** 0.01) ///
scalars("controls Controls " "umean Untreated $\bar{Y}$" "N Observations" "dstars \hline Differences  & \multicolumn{2}{c}{`dstars1'} & \multicolumn{2}{c}{`dstars2'} \\ %" "sestars \phantom{sestars} & \multicolumn{2}{c}{`sestars1'} & \multicolumn{2}{c}{`sestars2'} \\ %") ///
sfmt(%9.3f %9.3f %9.0fc %9.3f) mlabels(none) nonumbers prefoot(\hline)  posthead("`header'" "`emptyrow'" `"`titles1'"' "`titles2'" "`numbers'"  "`emptyrow'")   ///
refcat(treatedf "", nolabel below) postfoot(\hline\hline \\ \end{tabular} \\ \end{table}) fragment  nonotes replace
******************************************************************************************



*************************************************
*****TABLE A12
*************************************************



eststo clear
eststo M1: reg voted22 treatedfa, cluster(kunta19)
estadd local Controls "No"
estadd local Controls1 " "
estadd local Controls2 " "
estadd local Controls3 " "
estadd local Controls4 " "
estadd local Controls5 "No"
mean voted22 if e(sample)==1 & treatedfa==0
estadd scalar umean=r(table)[1,1]: M1
eststo M2: reg voted22 treatedfa molincome female age foreign, cluster(kunta19)
estadd local Controls "Female, Age, Immigrant,"
estadd local Controls1 "Ln Income"
estadd local Controls2 " "
estadd local Controls3 " "
estadd local Controls4 " "
estadd local Controls5 "No"
mean voted22 if e(sample)==1 & treatedfa==0
estadd scalar umean=r(table)[1,1]: M2
eststo M3:  reg voted22 treatedfa molincome female age foreign i.moses1d firstvote i.mohighschool, cluster(kunta19)
estadd local Controls "Female, Age, Immigrant,"
estadd local Controls1 "Ln Income,"
estadd local Controls2 "SES Background,"
estadd local Controls3 "Educational Background,"
estadd local Controls4 "First Time Eligble to Vote"
estadd local Controls5 "No"
mean voted22 if e(sample)==1 & treatedfa==0
estadd scalar umean=r(table)[1,1]: M3
eststo M4:  reg voted22 treatedfa molincome female age foreign i.moses1d firstvote i.mohighschool i.totpottreat, cluster(kunta19)
estadd local Controls "Female, Age, Immigrant,"
estadd local Controls1 "Ln Income,"
estadd local Controls2 "SES Background,"
estadd local Controls3 "Educational Background,"
estadd local Controls4 "First Time Eligble to Vote"
estadd local Controls5 "Yes"
mean voted22 if e(sample)==1 & treatedfa==0
estadd scalar umean=r(table)[1,1]: M4
local header "& \multicolumn{4}{c}{Outcome: Voted} \\"
local numbers "& (1) & (2) & (3) & (4) \\ \hline"
local emptyrow "& & & &  \\ "
local line "& & & & \hline \\ "

local dstars " "
local sestars " "
esttab, keep (treatedfa) noobs label se b(%9.3f) se(%9.3f) star(* 0.10 ** 0.05 *** 0.01) ///
scalars("Controls Controls" "Controls1 \phantom{controls}"  "Controls2 \phantom{controls}" "Controls3 \phantom{controls}" "Controls4 \phantom{controls}"  "Controls5 N Potentially Treated in HH" "umean Untreated $\bar{Y}$" "N Observations") ///
sfmt(%9.3f %9.3f %9.3f %9.3f  %9.3f  %9.3f  %9.3f %9.0fc) mlabels(none) nonumbers posthead("`header'" "`emptyrow'" "`numbers'" "`emptyrow'") ///
refcat(treatedf_i "", nolabel below) title("Average Treatment Effect") nonotes replace




